package com.huawei.springboot2023.business.user.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huawei.springboot2023.business.user.dto.LoginDto;
import com.huawei.springboot2023.business.user.dto.User;
import com.huawei.springboot2023.business.user.service.IUserService;

import com.huawei.springboot2023.core.dto.MyResult;
import com.huawei.springboot2023.core.util.MyRedisUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhouChengYang
 * @since 2023-04-05
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource(name = "userServiceImpl")
    private IUserService userService;

    @Autowired
    private MyRedisUtil redisUtil;

    /**
     *  检查是否需要行为验证码
     * @param dto 登录实体信息
     * @return 返回或转发信息行为验证码内容
     */
    @PostMapping("/checkIsUseXwyz")
    @ApiOperation("检查是否需要开启行为验证码")
    public MyResult checkIsUseXwyz(@RequestBody LoginDto dto){

        MyResult result = new MyResult();

        Object isUseXwyz = redisUtil.get("xwyz#" + dto.getUserName());

        if(isUseXwyz==null){// 不需要行为验证码
            dto.setIsChecked(true);
            result = doLogin(dto); // 进入登录
        }else{
            result.setMsg("需要开启行为验证码!");
            result.setCode("-4");
        }

        return result;
    }

    @PostMapping("/doLogin")
    @ResponseBody

    public MyResult doLogin(@RequestBody LoginDto loginDto){

        MyResult result = new MyResult();
        // 进行行为验证码的判断

        Object code = redisUtil.get("reqCode#" + loginDto.getLocalID());
        if(code==null){ //验证码过期
            result.setMsg("验证码过期");
            result.setCode("9999");
            return result;
        }else{ //验证码判断

            String sys_reqcode = (String) code;

            if (!loginDto.getReqCode().equalsIgnoreCase(sys_reqcode)){// 验证码错误
                result.setMsg("验证码错误！");
                result.setCode("9999");
                return result;
            }
        }

        if(!loginDto.getIsChecked()){
            result.setMsg("非法操作！");
            result.setCode("9999");
            return result;
        }
        result = userService.doLogin(loginDto);
        return result;
    }


    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("用户-注册")
    public MyResult add(@RequestBody User user){

        MyResult result = new MyResult();



        user.setPwd(DigestUtil.md5Hex(user.getPwd()));

        String userName = user.getUserName();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userName",userName);
        User one = userService.getOne(queryWrapper);
        if (one != null) {
            result.setMsg("用户名已存在！");
            result.setCode("9999");
            return result;
        }

        try {
            boolean save = userService.save(user);
            if(save){
                result.setMsg("添加成功！");
            }else{
                result.setMsg("添加失败！");
                result.setCode("9999");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("新增异常！");
            result.setCode("9999");
        }


        return result;

    }


}
